class Admin::ProductImagesController < Admin::BaseController

  before_filter :find_product

  def show
    redirect_to new_admin_product_image_path(@product)
  end

  def new
    # Just show the view
  end

  def create
    
    if params[:type] == '1'
      if params[:file]
        if @product.image.upload_file(params[:file])

          flash[:notice] = 'De afbeelding is met succes geupload.'

          return redirect_to edit_admin_product_path(@product)
        else
          flash[:notice] = 'Het geleverde bestand kan niet verwerkt worden.'
        end
      else
        flash[:notice] = 'Er is geen bestand opgegeven.'
      end

    elsif params[:type] == '2'
      if params[:url].size > 0
        
        if @product.image.upload_url(params[:url])
          return redirect_to edit_admin_product_path(@product)
        else
          flash[:notice] = 'Dit bestand kon niet geupload worden.'
        end
        
      else
        flash[:notice] = 'Er is geen URL opgegeven.'
      end
    end


    render :action => 'new'
  end

  def destroy
    @product.image.destroy

    flash[:notice] = 'De afbeelding is verwijderd.'

    redirect_to edit_admin_product_path(@product)
  end

  private
    def find_product
      @product = Product.find(params[:product_id])
    end

end